package com.seqb.manage.reptile.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.dict.service.DictService;
import com.seqb.manage.reptile.domain.ReptileDataDO;
import com.seqb.manage.reptile.service.ReptileDataService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lilinshen
 * @title 爬取数据表
 * @description 请填写相关描述
 * @date 2020-01-16
 */
@Controller
@RequestMapping("/manage/reptile/reptileData")
public class ReptileDataController extends BaseController {
    @Autowired
    private DictService dictService;
    @Autowired
    private ReptileDataService reptileDataService;

    @GetMapping()
    @RequiresPermissions("manage:reptile:reptileData:list")
    @Log("爬取数据表列表")
    String ReptileData(Model model) {
        model.addAttribute("method", dictService.getDictCache("request_method"));
        model.addAttribute("resultType", dictService.getDictCache("request_result_type"));
        return "manage/reptile/reptileData.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:reptile:reptileData:list")
    @Log("爬取数据表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<ReptileDataDO> page = reptileDataService.list(query);
        return new PageUtils(page.getResult(), page.getTotal());
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:reptile:reptileData:add")
    @Log("爬取数据表添加")
    String add(Model model) {
        model.addAttribute("method", dictService.getDictCache("request_method"));
        model.addAttribute("resultType", dictService.getDictCache("request_result_type"));
        return "manage/reptile/reptileData.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:reptile:reptileData:edit")
    @Log("爬取数据表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        ReptileDataDO reptileData = reptileDataService.get(id);
        model.addAttribute("reptileData", reptileData);
        model.addAttribute("method", dictService.getDictCache("request_method"));
        model.addAttribute("resultType", dictService.getDictCache("request_result_type"));
        return "manage/reptile/reptileData.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:reptile:reptileData:add")
    @Log("爬取数据表添加保存")
    public ResultVo save(ReptileDataDO reptileData) {
        if (reptileDataService.save(reptileData) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("爬取数据表编辑保存")
    @RequiresPermissions("manage:reptile:reptileData:edit")
    public ResultVo update(ReptileDataDO reptileData) {
        reptileDataService.update(reptileData);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:reptile:reptileData:remove")
    @Log("爬取数据表删除")
    public ResultVo remove(Long id) {
        if (reptileDataService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:reptile:reptileData:remove")
    @Log("爬取数据表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        reptileDataService.batchRemove(ids);
        return ResultVo.ok();
    }

    @ResponseBody
    @RequestMapping("/reptile")
    @Log("爬取数据表结果")
    @RequiresPermissions("manage:reptile:reptileData:reptile")
    ResultVo reptile(Long id) {
        return reptileDataService.reptile(id);
    }

    @ResponseBody
    @RequestMapping("/updatePaper")
    @Log("将爬取的结果更新到paper")
    @RequiresPermissions("manage:reptile:reptileData:updatePaper")
    ResultVo updatePaper(Long id, Long paperId) {
        return reptileDataService.updatePaper(id, paperId);
    }

}